package com.lqian.demo01;

/**
 * Thread.join的作用是保证线程执行结果的可见性
 *      ---> Waits for this thread to die.
 *      会阻塞主线程，直到这个线程结束
 *      本质是wait()和notify()
 *      ============================
 *      if (millis == 0) {
 *          while (isAlive()) {
 *          wait(0);
 *      }
 *  ============================
 */
public class Thread_Join {
    private static int i = 0;
    private static int x = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            //阻塞操作
            i = 1;
            x = 2;

        });

        Thread t2 = new Thread(()->{
            i = x + 2;
        });
        //thinking its sequence
        t1.start();
        //new Thread_Join().join(); 指向 对当前类调用方法的方法 >>> main
        //t1.join();
        t2.start();
        Thread.sleep(1000);
        System.out.println("result:"+i);







        //Thread.sleep(1000); //阻塞main 保证t1得到cpu的时间片
        //t1.join();保证t1线程的执行结果对于t2可见(t1线程一定要比t2线程优先执行)
        //t1.join() 阻塞的是主线程 why?

    }


    public final void join() throws InterruptedException {
        wait();
    }




}
